package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.NetworkTimeReqTO;
import com.handydata.antifraud.to.dap.NetworkTimeResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * 手机号入网时长查询(三网通用接口，不需要传运营商类型)
 */
@Slf4j
@Service("networkTimeWsService")
@Comment("手机号码在网时长")
public class NetworkTimeWsServiceImpl extends DataPlatformWsService<NetworkTimeReqTO, NetworkTimeResTO>
		implements WsInvocationService<NetworkTimeReqTO, NetworkTimeResTO> {

	@Override
	public NetworkTimeResTO query(NetworkTimeReqTO req) throws WsInvokeException {
		log.info("手机号码在网时长请求参数 {}", req);
		NetworkTimeResTO resTO = new NetworkTimeResTO();
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("手机号码在网时长参数校验 {}", "phone为空");
			resTO.setResCode("1111");
			resTO.setResMsg("手机号码在网时长接口phone参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.NET_WORK_TIME.getCode();
		resTO = executeQuery(req);
		// 行内压测挡板处理
//		try {
//			Thread.sleep(250);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		//模拟数据源返回成功结果      success
		// success
//		String data = "{\"resCode\":\"0000\",\"resMsg\":\"查询成功\",\"netTime\":\"24个月及以上\"}";
		// fail
//		String data = "{\"resCode\":\"0000\",\"resMsg\":\"查询成功\",\"netTime\":\"0-3个月\"}";
//		JSONObject jasonObject = JSONObject.parseObject(data);
//		resTO.setNetTime(jasonObject.getString("netTime"));
//		resTO.setResCode(jasonObject.getString("resCode"));
//		resTO.setResMsg(jasonObject.getString("resMsg"));
		// 挡板入库
//		DataCallLogsReqBO dataCallLogsReqBO = new DataCallLogsReqBO();
//		dataCallLogsReqBO.setId(UUID.randomUUID().toString().replace("-", ""));
//		dataCallLogsReqBO.setParams(JSONObject.toJSONString(req));
//		dataCallLogsReqBO.setElapsed(250);
//		dataCallLogsReqBO.setWselapsed(250);
//		dataCallLogsReqBO.setDataType(this.dataType);
//		dataCallLogsReqBO.setResCode("0000");
//		dataCallLogsReqBO.setResMsg("查询成功");
//		dataCallLogsReqBO.setResult(data);
//		dataCallLogsReqBO.setOrderId(req.getOrderId());
//		dataCallLogsReqBO.setUpdateTime(new Date());
//		dataCallLogsReqBO.setCreateTime(new Date());
//		dataCallLogsService.createDataCallLogs(dataCallLogsReqBO);
		return resTO;
	}

	@Override
	protected String createQueryXml(NetworkTimeReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>1.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>100081</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<phone>").append(req.getPhoneNum()).append("</phone>");
		xml.append("</subatm>");
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected NetworkTimeResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用手机号入网时长查询接口返回报文：{}", resultXml);
		NetworkTimeResTO resTO = new NetworkTimeResTO();
		try {
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {

				if ("0000".equals(jasonObject.get("resCode"))) {
					resTO.setNetTime((String)jasonObject.get("netTime"));
				}
				resTO.setResCode((String)jasonObject.get("resCode"));
				resTO.setResMsg((String)jasonObject.get("resMsg"));
				//resTO.setReqId(dataCallLogsReqBO.getId());
			} else {
				resTO.setResCode("1111");
				resTO.setResMsg("手机号入网时长查询接口返回数据异常");
			}
		} catch (Exception e) {
			log.info("调用手机号入网时长查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("手机号入网时长查询接口返回数据异常");
		}
		log.info("调用手机号入网时长查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, NetworkTimeReqTO.class);
	}

}


